package com.xoms.pad.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.xoms.pad.controller.res.BaseResult;
import com.xoms.pad.controller.res.ObjectResult;
import com.xoms.pad.controller.res.bean.RAccountLoginBean;
import com.xoms.pad.controller.res.bean.RVertifyVCodeBean;
import com.xoms.pad.controller.springmvc.req.AuthLoginParam;
import com.xoms.pad.controller.springmvc.req.CheckTokenParam;
import com.xoms.pad.controller.springmvc.req.FindPasswordParam;
import com.xoms.pad.controller.springmvc.req.LoginParam;
import com.xoms.pad.controller.springmvc.req.SendVCodeParam;
import com.xoms.pad.controller.springmvc.req.UpdatePasswordParam;
import com.xoms.pad.controller.springmvc.req.VertifyVCodeParam;
import com.xoms.pad.model.AccountBean;
import com.xoms.pad.service.AccountService;
import com.xoms.pad.service.exception.ServiceException;
import com.xoms.util.NumUtil;
import com.xoms.util.StringUtil;

@Component
public class AccountController {
	
	Logger logger = Logger.getLogger(this.getClass());
	

	@Autowired
	AccountService accountServiceImpl;

	/**
	 * 验证用户token
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ObjectResult<RAccountLoginBean> checkToken(CheckTokenParam param, HttpServletRequest request, HttpServletResponse response) {
		ObjectResult<RAccountLoginBean> result = new ObjectResult<>();
		try {
			// 获取传递的token
			String token = StringUtil.isEmpty(param.getToken()) ? "" : param.getToken();
			// 根据token获取账号信息
			RAccountLoginBean data = accountServiceImpl.getAccountByToken(token);
			result.setData(data);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}
	
	/**
	 * 账号登录
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ObjectResult<RAccountLoginBean> login(LoginParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ObjectResult<RAccountLoginBean> result = new ObjectResult<>();
		try {
			// 获取手机号和密码
			String phoneNum = param.getPhone();
			String password = param.getPassword();
			// 验证登录并返回用户信息
			RAccountLoginBean data = accountServiceImpl.login(phoneNum, password);
			result.setData(data);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}
	
	/**
	 * 发送验证码
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public BaseResult sendVCode(SendVCodeParam param, HttpServletRequest request, HttpServletResponse response) {
		BaseResult result = new BaseResult();
		try {
			int type = NumUtil.toInt(param.getType(), 1);//type:1 找回密码
			// 根据type发送手机验证码
			boolean flag = accountServiceImpl.sendVCode(type, param.getPhone());
			result.setFlag(flag);
		}  catch (ServiceException e) {
			result.setServiceException(e);
			logger.error(e.getMessage(), e);
		} catch (Exception e) {
			result.setFlag(false);
			logger.error(e.getMessage(), e);
		}
		return result;
	}
	
	/**
	 * 验证验证码
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ObjectResult<RVertifyVCodeBean> vertifyVCode(VertifyVCodeParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ObjectResult<RVertifyVCodeBean> result = new ObjectResult<RVertifyVCodeBean>();
		try {
			int type = NumUtil.toInt(param.getType(), 1);// type：1，验证找回密码验证码
			// 验证成功返回临时token，根据token更新密码
			RVertifyVCodeBean data = accountServiceImpl.vertifyVCode(type, param.getPhone(), param.getVcode());
			result.setData(data);
			result.setFlag(data != null);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}
	
	/**
	 * 忘记密码，验证手机后，更新密码
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public BaseResult findPassword(FindPasswordParam param, HttpServletRequest request, HttpServletResponse response) {
		BaseResult result = new BaseResult();
		try {
			// 找回密码时验证手机验证码后的临时token
			String tempToken = param.getTempToken();
			// 新密码
			String pwd = param.getPwd();
			String submitPwd = param.getSubmitPwd();

			boolean flag = accountServiceImpl.findPassword(tempToken, pwd, submitPwd);
			result.setFlag(flag);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}
	
	/**
	 * 更新密码
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public BaseResult updatePassword(UpdatePasswordParam param, HttpServletRequest request,
			HttpServletResponse response) {
		BaseResult result = new BaseResult();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");
			// 账号个人更新密码：输入旧密码和新密码
			String oldPwd = param.getPassword();
			String newPwd = param.getNewPassword();
			String submitPwd = param.getSubmitPassword();
			// 返回更新是否成功
			boolean flag = accountServiceImpl.updatePassword(account, oldPwd, newPwd, submitPwd);
			result.setFlag(flag);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}
	
	/**
	 * 授权登录，有些操作需要运营经理登录授权，判断登录账号是否有授权权限
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public BaseResult authLogin(AuthLoginParam param, HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		return null;
	}

}
